import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

class TrafficLightSystem {
    private Queue<String> mainRoadQueue;
    private Queue<String> sideRoadQueue;
    private boolean mainRoadGreen;
    private boolean sideRoadGreen;
    private int timer;

    public TrafficLightSystem() {
        mainRoadQueue = new LinkedList<>();
        sideRoadQueue = new LinkedList<>();
        mainRoadGreen = true;
        sideRoadGreen = false;
        timer = 0;
    }

    public void addCarToMainRoad() {
        mainRoadQueue.offer("Car");
    }

    public void addCarToSideRoad() {
        sideRoadQueue.offer("Car");
    }

    public void switchLights() {
        if (!mainRoadQueue.isEmpty() && !sideRoadQueue.isEmpty()) {
            mainRoadGreen = false;
            sideRoadGreen = true;
        } else {
            mainRoadGreen = true;
            sideRoadGreen = false;
        }
    }

    public void simulate(int duration) throws InterruptedException {
        while (timer < duration) {
            // Simulate cars arriving and departing
            if (Math.random() < 0.3) {
                addCarToMainRoad();
            }
            if (Math.random() < 0.2) {
                addCarToSideRoad();
            }

            // Switch lights if necessary
            if (timer % 30 == 0) {
                switchLights();
            }

            // Print current status
            System.out.println("Time: " + timer + "s");
            System.out.println("Main Road Queue: " + mainRoadQueue);
            System.out.println("Side Road Queue: " + sideRoadQueue);
            System.out.println("Main Road Green: " + mainRoadGreen);
            System.out.println("Side Road Green: " + sideRoadGreen);
            System.out.println("-----------------------------------");

            // Simulate time passing
            Thread.sleep(1000);
            timer++;
        }
    }
}

public class Main {
    public static void main(String[] args) throws InterruptedException {
        TrafficLightSystem trafficSystem = new TrafficLightSystem();
        trafficSystem.simulate(600); // Run for 10 minutes (600 seconds)
    }
}
